﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Dommel;
using Homework.Repository;
using LY.OAWeb.Repository.IRepository;

namespace LY.OAWeb.Repository.Repository
{
    /// <summary>
    /// 仓储抽象基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        protected IDbConnection _db = DapperFactory.GetInstance().GetDbConnection();
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool Delete(T entity)
        {
            return _db.Delete(entity);
        }
        /// <summary>
        /// 删除指定ID的数据
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public bool DeleteById(object objId)
        {
            return Delete(_db.Get<T>(objId));
        }
        /// <summary>
        /// 根据主键值查询单条数据
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public T GetById(object objId)
        {
            return _db.Get<T>(objId);
        }
        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<T> GetList()
        {
            return _db.GetAll<T>().ToList();
        }
        /// <summary>
        /// 添加一条实体数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int Insert(T entity)
        {
            return (int)_db.Insert(entity);
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool Update(T entity)
        {
            return _db.Update(entity);
        }
    }
}
